package com.oriental.manage.service.specially.Impl;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.utils.DateUtils;
import com.oriental.manage.service.specially.RiskSearchService;
import com.oriental.riskfront.api.model.CommercialRiskBody;
import com.oriental.riskfront.api.model.PersonalRiskBody;
import com.oriental.riskfront.api.model.specially.ResponseModel;
import com.oriental.riskfront.api.service.RiskSearchInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by guzhe on 2017/4/20.
 */
@Slf4j
@Service
class RiskSearchServiceImpl implements RiskSearchService {
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private RiskSearchInterface riskSearchInterface;

    @Override
    public void queryPersonRiskPage(Pagination<PersonalRiskBody> pagination, PersonalRiskBody personRiskInfo) throws Exception {
        if (StringUtils.isNotBlank(personRiskInfo.getOccurtimeb())) {
            Date d = format.parse(personRiskInfo.getOccurtimeb().replace("Z", " UTC"));//注意是空格+UTC
            personRiskInfo.setOccurtimeb(DateUtils.format(d, DateUtils.DATESHOWFORMAT));
        }if (StringUtils.isNotBlank(personRiskInfo.getOccurtimee())) {
            Date d = format.parse(personRiskInfo.getOccurtimee().replace("Z", " UTC"));//注意是空格+UTC
            personRiskInfo.setOccurtimee(DateUtils.format(d, DateUtils.DATESHOWFORMAT));
        }
        ResponseModel<List<PersonalRiskBody>> responseModel = riskSearchInterface.queryPersonRiskList(personRiskInfo);//掉用接口
        if (responseModel.isSuccess()) {
            List<PersonalRiskBody> list = responseModel.getResult();
            if (list != null && list.size() > 0) {
                pagination.setRowCount(list.size());
                pagination.setList(list);
            }else{
                pagination.setList(null);
            }
        } else {
            throw new Exception(responseModel.getErrorMsg());
        }
    }

    @Override
    public void queryMerchantPage(Pagination<CommercialRiskBody> pagination, CommercialRiskBody commercialRiskInfo) throws Exception {
        if (StringUtils.isNotBlank(commercialRiskInfo.getOccurtimeb())) {
            Date d = format.parse(commercialRiskInfo.getOccurtimeb().replace("Z", " UTC"));//注意是空格+UTC
            commercialRiskInfo.setOccurtimeb(DateUtils.format(d, DateUtils.DATESHOWFORMAT));
        }if (StringUtils.isNotBlank(commercialRiskInfo.getOccurtimee())) {
            Date d = format.parse(commercialRiskInfo.getOccurtimee().replace("Z", " UTC"));//注意是空格+UTC
            commercialRiskInfo.setOccurtimee(DateUtils.format(d, DateUtils.DATESHOWFORMAT));
        }
        ResponseModel<List<CommercialRiskBody>> responseModel = riskSearchInterface.queryMerchantRiskList(commercialRiskInfo);//掉用接口
        if (responseModel.isSuccess()) {
            List<CommercialRiskBody> list = responseModel.getResult();
            if (list != null && list.size() > 0) {
                pagination.setRowCount(list.size());
                pagination.setList(list);
            }else{
                pagination.setList(null);
            }
        } else {
            throw new Exception(responseModel.getErrorMsg());
        }
    }

}
